package com.mind.ridge.service.front.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mind.ridge.mapper.front.ShoppingCartMapper;
import com.mind.ridge.pojo.front.ShoppingCart;
import com.mind.ridge.service.front.ShoppingCartService;
import com.mind.ridge.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author Mind
 */
@Service
@Slf4j
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {
    @Override
    public R<String> add(ShoppingCart shoppingCart, HttpSession session) {
        if (shoppingCart == null) {
            return R.error("ERROR");
        }
        //获取当前用户的id
        Long userId = (Long) session.getAttribute("userId");
        shoppingCart.setUserId(userId);

        //查询该用户的购物车是否已含有该菜品或套餐，若含有则不再添加新的记录
        //只需将原有的记录加1
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(shoppingCart.getDishId() == null, ShoppingCart::getSetmealId,
                shoppingCart.getSetmealId());
        lqw.eq(shoppingCart.getSetmealId() == null, ShoppingCart::getDishId,
                shoppingCart.getDishId());
        lqw.eq(ShoppingCart::getUserId, userId);
        ShoppingCart cart = this.getOne(lqw);
        if (cart == null) {
            //若不存在，则将该菜品添加到数据库，并设置数量为1
            shoppingCart.setNumber(1);
            this.save(shoppingCart);
        } else {
            //若存在，则在原有的数量基础下+1
            cart.setNumber(cart.getNumber() + 1);
            log.info(cart.toString());
            this.updateById(cart);
        }
        return R.success("SUCCESS");
    }

    @Override
    public R<List<ShoppingCart>> cartList(HttpSession session) {
        //获取当前登录用户的id
        Long userId = (Long) session.getAttribute("userId");

        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();

        //根据id查询其购物车订单信息
        lqw.eq(ShoppingCart::getUserId, userId);
        List<ShoppingCart> list = this.list(lqw);
        return R.success(list);
    }

    @Override
    public R<String> sub(ShoppingCart shoppingCart) {
        if (shoppingCart == null) {
            return R.error("ERROR");
        }
        //判断当前的餐品是菜品还是套餐
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId())
                .eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId());
        //获取到当前的餐品或套餐
        ShoppingCart cart = this.getOne(lqw);
        //获取当前餐品的数量
        Integer number = cart.getNumber();
        //如数量=1则删除该记录
        if (number == 1) {
            this.removeById(cart);
            return R.success("SUCCESS");
        }
        cart.setNumber(number - 1);
        boolean success = this.updateById(cart);
        return success ? R.success("SUCCESS") : R.error("ERROR");
    }
}
